package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.SysEmployee;
import com.ruoyi.system.service.ISysEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 人员管理Controller
 * 
 * @author ruoyi
 * @date 2021-05-26
 */
@Api(tags = "人员管理模块")
@RestController
@RequestMapping("/system/employee")
public class SysEmployeeController extends BaseController
{
    @Autowired
    private ISysEmployeeService sysEmployeeService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询人员管理列表
     */
    @ApiOperation("查询人员管理列表")
//    @PreAuthorize("@ss.hasPermi('system:employee:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysEmployee sysEmployee)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        sysEmployee.setDeptId(null == sysEmployee.getDeptId() ? user.getDeptId() : sysEmployee.getDeptId());
        List<SysEmployee> list = sysEmployeeService.selectSysEmployeeList(sysEmployee);
        return getDataTable(list);
    }

    /**
     * 导出人员管理列表
     */
//    @PreAuthorize("@ss.hasPermi('system:employee:export')")
    @Log(title = "人员管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysEmployee sysEmployee)
    {
        List<SysEmployee> list = sysEmployeeService.selectSysEmployeeList(sysEmployee);
        ExcelUtil<SysEmployee> util = new ExcelUtil<SysEmployee>(SysEmployee.class);
        return util.exportExcel(list, "人员管理数据");
    }

    /**
     * 获取人员管理详细信息
     */
    @ApiOperation("获取人员管理详细信息")
//    @PreAuthorize("@ss.hasPermi('system:employee:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysEmployeeService.selectSysEmployeeById(id));
    }

    /**
     * 新增人员管理
     */
    @ApiOperation("新增人员管理")
//    @PreAuthorize("@ss.hasPermi('system:employee:add')")
    @Log(title = "人员管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysEmployee sysEmployee)
    {
        if (StringUtils.isNotEmpty(sysEmployee.getPhone())
                && sysEmployeeService.checkPhoneUnique(sysEmployee.getPhone())>0)
        {
            return AjaxResult.error("新增人员['" + sysEmployee.getName() + "']失败，手机号码已存在");
        }else{
            return toAjax(sysEmployeeService.insertSysEmployee(sysEmployee));
        }
    }

    /**
     * 修改人员管理
     */
    @ApiOperation("修改人员管理")
//    @PreAuthorize("@ss.hasPermi('system:employee:edit')")
    @Log(title = "人员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysEmployee sysEmployee)
    {
        return toAjax(sysEmployeeService.updateSysEmployee(sysEmployee));
    }

    /**
     * 删除人员管理
     */
    @ApiOperation("删除人员管理")
//    @PreAuthorize("@ss.hasPermi('system:employee:remove')")
    @Log(title = "人员管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysEmployeeService.deleteSysEmployeeByIds(ids));
    }
}
